package com.willren;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.Window;
import android.view.WindowManager;

public class DefaultAlertDialog {

	public enum DialogType {
		DialogType_Waitting, 
		DialogType_SingleSelect, 
		DialogType_AlternativeSelect
	}
	
	public enum ClickType {
		ClickType_POSITIVE, 
		ClickType_NEGATIVE
	}
	
	public interface OnClickListener {
		public void onClick(ClickType type);
	}

	private boolean isResetPosition;

	private DialogType type;
	private AlertDialog dialog;

	public DefaultAlertDialog(Context context, DialogType type) {

		this.type = type;

		// Create dialog
		Builder builder = new AlertDialog.Builder(context);
		dialog = builder.create();

		dialog.setCancelable(false);
		
		// Set dialog position
		if (isResetPosition) {
			Window w = dialog.getWindow();
			WindowManager.LayoutParams lp = w.getAttributes();
			lp.x = 10;
			lp.y = 25;
			lp.alpha = 1.0f;
			lp.dimAmount = 1.0f;
			w.setAttributes(lp);
			w.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
					WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		}
	}
	
	public void setIcon(Drawable icon) {
		dialog.setIcon(icon);
	}

	public void setText(String title, String prompt, String positive,
			String negative, String neutral, OnClickListener listener) {
		
		final OnClickListener finalListener = listener;
		
		dialog.setTitle(title);
		dialog.setMessage(prompt);

		switch (type) {
		case DialogType_Waitting:
			break;
		case DialogType_SingleSelect:
			dialog.setButton(DialogInterface.BUTTON_POSITIVE, positive, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (null != finalListener) {
						finalListener.onClick(ClickType.ClickType_POSITIVE);
					}
				}
			});
			break;
		case DialogType_AlternativeSelect:
			dialog.setButton(DialogInterface.BUTTON_POSITIVE, positive, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (null != finalListener) {
						finalListener.onClick(ClickType.ClickType_POSITIVE);	
					}
				}
			});
			dialog.setButton(DialogInterface.BUTTON_NEGATIVE, negative, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (null != finalListener) {
						finalListener.onClick(ClickType.ClickType_NEGATIVE);	
					}
				}
			});
			break;
		}
	}

	public void show(boolean isShow) {
		if (isShow) {
			dialog.show();
		} else {
			dialog.dismiss();
		}
	}
}
